package qlsl.androiddesign.view.subview.activityview;

import qlsl.androiddesign.activity.subactivity.PostingCreateActivity;
import qlsl.androiddesign.activity.subactivity.TestVpVpActivity;
import qlsl.androiddesign.adapter.commonadapter.ViewPagerFragmentAdapterTitle;
import qlsl.androiddesign.application.SoftwareApplication;
import qlsl.androiddesign.fragment.subfragment.TvFragment;
import qlsl.androiddesign.view.baseview.FunctionView;
import qlsl.androiddesign.view.commonview.PagerSlidingTab;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 测试vp包含vp页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class TestVpVpView extends FunctionView<TestVpVpActivity> {

	public static final String KEY_CLASS_NAME = "className";
	public static final String KEY_ARGS = "args";

	public ViewPagerFragmentAdapterTitle mBaseFragmentViewPageAdapter;

	public PagerSlidingTab mSlidingTab;// PagerSlidingTab

	ViewPager mViewPager;

	public TestVpVpView(TestVpVpActivity activity) {
		super(activity);
		setContentView(R.layout.activity_test_vp_vp);
	}

	protected void initView(View view) {
		setTitle("二手车配件");
		mSlidingTab = findViewById(R.id.slidingTab);
		mViewPager = findViewById(R.id.pager);

		// 1.封装adapter, 注意这里是继承的FragmentPagerAdapter,
		// 并且传入的是getChildFragmentManager()
		mBaseFragmentViewPageAdapter = new ViewPagerFragmentAdapterTitle(
				SoftwareApplication.getInstance(),
				activity.getSupportFragmentManager());
		// 2.添加page页
		addPageToAdapter(mBaseFragmentViewPageAdapter);

		// 3.设置adapter
		mViewPager.setAdapter(mBaseFragmentViewPageAdapter);
		// 4.绑定viewPager到Indicator
		mSlidingTab.setViewPager(mViewPager);
		setRightButtonResource(R.drawable.forum_posth);
		showRightButton();

		// mSlidingTab.setOnPageChangeListener(new PageListener1());

		// setScreenPageLimit(mViewPager);
	}

	private void addPageToAdapter(ViewPagerFragmentAdapterTitle fragmentAdapter) {
		String[] titles = activity.getResources().getStringArray(
				R.array.news_viewpage_arrays);
		// String[] titlessss = App.getAppContext().getResources().getString();
		// 添加page,并给fragment传入对应的bundle参数，在请求接口的时候用
		fragmentAdapter.addPage(titles[0], TvFragment.class,
				getBundle(titles[0]));
		fragmentAdapter.addPage(titles[1], TvFragment.class,
				getBundle(titles[1]));
		fragmentAdapter.addPage(titles[2], TvFragment.class,
				getBundle(titles[2]));
		fragmentAdapter.addPage(titles[3], TvFragment.class,
				getBundle(titles[3]));
		fragmentAdapter.addPage(titles[4], TvFragment.class,
				getBundle(titles[4]));
		fragmentAdapter.addPage(titles[5], TvFragment.class,
				getBundle(titles[5]));
		fragmentAdapter.addPage(titles[6], TvFragment.class,
				getBundle(titles[6]));
		fragmentAdapter.addPage(titles[7], TvFragment.class,
				getBundle(titles[7]));
		fragmentAdapter.addPage(titles[8], TvFragment.class,
				getBundle(titles[8]));

	}

	/**
	 * 基类会根据不同的catalog展示相应的数据
	 * 
	 * @param catalog
	 *            要显示的数据类别
	 * @return
	 */
	public Bundle getBundle(String catalog) {
		Bundle bundle = new Bundle();
		bundle.putString("11", catalog);
		return bundle;
	}

	public Bundle getBundle(int newType) {
		Bundle bundle = new Bundle();
		bundle.putInt("11", newType);
		return bundle;
	}

	/**
	 * 设置ViewPager能够缓存的页数
	 */
	protected void setScreenPageLimit(ViewPager mViewPager) {
		mViewPager
				.setOffscreenPageLimit(mViewPager.getAdapter().getCount() - 1);
	}

	protected void initData() {

	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_right:
			startActivityForResult(PostingCreateActivity.class, 0);
			break;

		default:
			break;
		}

	}

	public void onActivityResult(int arg0, int arg1, Intent intent) {
		if (hasRefresh(intent)) {
			setRefresh(true);
			// query();
			showToast("来了一拨新帖");
		}
	}

}
